#include "sharedmemory.hpp"


int main()
{
    My_Shared_Memory::SharedMemory myshm(FIFO_NAME, 400, IPC_CREAT);
    int *myarr = (int *)myshm.Get();
     std::cout << &myarr << std::endl;
    int record = 0;
    int flag = 0;
    while (1)
    {
       sleep(1);
        std::cout << myarr[0] << std::endl;
        if (record == myarr[0])
        {
            ++flag;
            if (flag == 2)
            {
                break;
            }
        }
        else
        {
            flag = 0;
            record = myarr[0];
        }
    }
    return 0;
}








int client()
{
    int myshmarr[100];
    // 获得KEY
    key_t mykey = ftok(FIFO_PATHNAME, 'A');
    if (mykey <= 0)
    {
        ERROR_MESSAGE("ftok");
    }

    // 创建共享内存
    umask(0);
    int myshmid = shmget(mykey, sizeof(myshmarr), IPC_CREAT);
    if (myshmid == -1)
    {
        ERROR_MESSAGE("shmget");
    }

    // 附加共享内存
    int *newshared = (int *)shmat(myshmid, nullptr, 0);
    if (newshared == (int *)-1)
    {
        ERROR_MESSAGE("shmat");
    }
    std::cout << &newshared << std::endl;
    std::cout << &myshmarr << std::endl;
    int record = 0;
    int flag = 0;
    while (1)
    {
        sleep(1);
        std::cout << newshared[0] << std::endl;
        if (record == newshared[0])
        {
            ++flag;
            if (flag == 2)
            {
                break;
            }
        }
        else
        {
            flag = 0;
            record = newshared[0];
        }
    }
    // 共享内存分离
    shmdt(newshared);

    return 0;
}
